package com.baiyimi.pan.server.controller;


import com.baiyimi.pan.server.service.AccountService;
import com.baiyimi.pan.server.service.FileService;
import com.baiyimi.pan.server.service.FolderViewService;
import com.baiyimi.pan.server.service.ServerInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("homeController")
public class HomeController {
    private static final String CHARSET_BY_AJAX = "text/html; charset=utf-8";
    @Resource
    private ServerInfoService si;

    @Resource
    private FolderViewService fvs;

    @Resource
    private AccountService as;

    @Resource
    private FileService fis;

    /**
     * <h2>应答机制</h2>
     * <p>
     * 该机制旨在防止某些长耗时操作可能导致Session失效的问题（例如上传、视频播放等），方便用户持续操作。
     * </p>
     *
     * @return String “pong”或“”
     * @author baiyimi
     */
    @RequestMapping(value = {"ping.ajax"}, produces = {CHARSET_BY_AJAX})
    public String pong(final HttpServletRequest request) {
        return as.doPong(request);
    }

    @RequestMapping(value = {"/douploadFile.ajax"}, produces = {CHARSET_BY_AJAX})
    public String douploadFile(final HttpServletRequest request, final HttpServletResponse response, final MultipartFile file) {
        return this.fis.doUploadFile(request, response, file);
    }

    @RequestMapping(value = {"checkUploadFile.ajax"}, produces = {CHARSET_BY_AJAX})
    public String checkUploadFile(final HttpServletRequest request, final HttpServletResponse response) {
        return this.fis.checkUploadFile(request, response);
    }


    @RequestMapping({"doLogin.ajax"})
    public String doLogin(final HttpServletRequest request, final HttpSession session) {
        return this.as.checkLoginRequest(request, session);
    }

    // 获取一个新验证码并存入请求者的Session中
    @RequestMapping({"getNewVerCode.do"})
    public void getNewVerCode(final HttpServletRequest request, final HttpServletResponse response, final HttpSession session) {
        as.getNewLoginVerCode(request, response, session);
    }

    @RequestMapping(value = {"getPublicKey.ajax"}, produces = {CHARSET_BY_AJAX})
    public String getPublicKey() {
        return this.as.getPublicKey();
    }

    @RequestMapping(value = {"getFolderView.ajax"}, produces = {CHARSET_BY_AJAX})
    public String getFolderView(final String fid, final HttpSession session, final HttpServletRequest request) {
        return fvs.getFolderViewToJson(fid, session, request);
    }

    @RequestMapping({"getServerOS.ajax"})
    public String getServerOS() {
        return this.si.getOSName();
    }

}
